package com.ruoyi.paiban.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.page.TableDataInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.paiban.domain.Paiban;
import com.ruoyi.paiban.service.IPaibanService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 员工排班Controller
 * 
 * @author beilian
 * @date 2023-03-31
 */
@Controller
@RequestMapping("/paiban/paibanTest")
public class PaibanController extends BaseController
{
    private String prefix = "paiban/paibanTest";

    @Autowired
    private IPaibanService paibanService;

    @RequiresPermissions("paiban:paibanTest:view")
    @GetMapping()
    public String paibanTest()
    {
        return prefix + "/paibanTest";
    }

    /**
     * 查询员工排班列表
     */
    @RequiresPermissions("paiban:paibanTest:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Paiban> list(String year,String month, HttpServletRequest request)
    {
        Paiban paiban1 = new Paiban();
        paiban1.setId(10L);
        List<Paiban> test = paibanService.selectPaibanList2monthandyear(month,year);

        return test;

    }
//    public List<Paiban> list(Paiban paiban1, HttpServletRequest request)
//    {
//        paiban1 = new Paiban();
//        List<Paiban> test = paibanService.selectPaibanList(paiban1);
//
//        return test;
//
//    }
    /**
     * 查询员工排班列表
     */
    @PostMapping("/list2oneDay")
    @ResponseBody
    public TableDataInfo list2oneday(HttpServletRequest request)
    {
        String year = request.getSession().getAttribute("year").toString();
        int fy = Integer.parseInt(year)-1900;
        String month = request.getSession().getAttribute("month").toString();
        int fm = Integer.parseInt(month)-1;
        String day = request.getSession().getAttribute("day").toString();
        int fd = Integer.parseInt(day);

        Date date = new Date(fy,fm,fd,0,0,0);
        Paiban paiban = new Paiban();
        paiban.setDate(date);
        List<Paiban> test = paibanService.selectPaibanList(paiban);
        return getDataTable(test);

    }

    @PostMapping("/datalist")
    @ResponseBody
    public String datalist(String paiban, HttpServletRequest request)
    {
        return "aaaaaa";
    }


    /**
     * 按日期查询员工排班列表
     */



    /**
     * 导出员工排班列表
     */
    @RequiresPermissions("paiban:paibanTest:export")
    @Log(title = "员工排班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Paiban paiban)
    {
        List<Paiban> list = paibanService.selectPaibanList(paiban);
        ExcelUtil<Paiban> util = new ExcelUtil<Paiban>(Paiban.class);
        return util.exportExcel(list, "员工排班数据");
    }

    /**
     * 新增员工排班
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存员工排班
     */
    @RequiresPermissions("paiban:paibanTest:add")
    @Log(title = "员工排班", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Paiban paiban)
    {
        return toAjax(paibanService.insertPaiban(paiban));
    }

    /**
     * 修改员工排班
     */
    @RequiresPermissions("paiban:paibanTest:edit")
    @GetMapping("/edit/{year}/{month}/{day}")
    public String edit(@PathVariable("year") String year,@PathVariable("month") String month,@PathVariable("day") String day, ModelMap mmap,
                       HttpServletRequest request)
    {
        String date = "";
        Paiban paiban = paibanService.selectPaibanBydate(date);
        mmap.put("paiban", paiban);
        request.getSession().setAttribute("year",year);
        request.getSession().setAttribute("month",month);
        request.getSession().setAttribute("day",day);

        return prefix + "/edit";
    }

    /**
     * 修改保存员工排班
     */
    @RequiresPermissions("paiban:paibanTest:edit")
    @Log(title = "员工排班", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Paiban paiban)
    {
        return toAjax(1);
    }


    @PostMapping("/IDreturn/{year}/{month}/{day}")
    @ResponseBody
    public String IDreturn(@PathVariable("year") String year,@PathVariable("month") String month,@PathVariable("day") String day)
    {
        String date = "2023-4-1";
        Paiban paiban = paibanService.selectPaibanBydate(date);
        return paiban.getId().toString();
    }
    //IDreturn
    /**
     * 删除员工排班
     */
    @RequiresPermissions("paiban:paibanTest:remove")
    @Log(title = "员工排班", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String date)
    {
        return toAjax(paibanService.deletePaibanByIds(date));
    }


    @GetMapping("/qiandao")
    public String qiandao(HttpServletRequest request){
        return "/paiban/paibanTest/qiandao";
    }

//    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
//        Date date = new Date(System.currentTimeMillis());
//        tdate = formatter.format(date);
//        uname  = request.getSession().getAttribute("name4user");
@PostMapping("/qiandao")
public AjaxResult set(HttpServletRequest request) throws ParseException {

    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date ttdate = new Date(System.currentTimeMillis());

        String tdate = formatter.format(new Date());
        String uname  = request.getSession().getAttribute("name4user").toString();
        Date date =  formatter.parse(tdate);

        Paiban p =  new Paiban();
        p.setDate(date);
        p.setYuangongName(uname);
        p.setQiandaoStatus("1");
        System.out.println("qqq1"+date);
        System.out.println("qqq2"+uname);

    return toAjax(paibanService.updataqiandao(p));
    }





}
